using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json;
using NiMVC;
using UnityEditor;
using UnityEngine;


[CustomEditor(typeof(BaseModel),true)]
// [CanEditMultipleObjects]
public class BaseModelEditor : Editor
{
    private BaseModel ttt;
    
    private void OnEnable()
    {
        ttt = target as BaseModel;
    }

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        if (GUILayout.Button("测试"))
        {
           
            var t = ttt.GetType();
            Debug.Log(t);
            拉取数据(ttt);
        }
        if (GUILayout.Button("逆推到Excel"))
        {
           
            var t = ttt.GetType();
            Debug.Log(t);

            var attri = t.GetCustomAttribute<ModelAttribute>();
            ExcelExchangeTools excel = new ExcelExchangeTools(attri.AssetPath, ExcelExchangeTools.OpType.ReadAndWrite);
            if(!excel.InitSuccess) return;
            
            
            var f = t.GetField("Datas", BindingFlags.NonPublic | BindingFlags.Instance);
            var v = f.GetValue(ttt);

            List<object> res = new List<object>();
            foreach (var item in (IEnumerable<dynamic>)v)
            {
                var a = item;
                res.Add(a);
            }
            
            Debug.LogError(JsonConvert.SerializeObject(res));
            excel.AddAllRow(res);
            
            
            int idX = -1;
            int idY = -1;
            var content = excel.GetJson(ref idX, ref idY);
            Debug.Log($"Excel 内容 结果： {content.Item2}：： idx = {idX};idy = {idY}  content={content.Item1}");
            

            
            excel.SaveExcel();
            
            ExcelExchangeTools excel2 = new ExcelExchangeTools(attri.AssetPath, ExcelExchangeTools.OpType.OnlyRead);
            var content2 = excel.GetJson(ref idX, ref idY);
            Debug.Log($"Excel 内容 结果： {content2.Item2}：： idx = {idX};idy = {idY}  content={content2.Item1}");
        }
    }

    public static void 拉取数据(BaseModel v)
    {
        Type t = v.GetType();
        var attri = t.GetCustomAttribute<ModelAttribute>();
        if(attri==null) return;
        
        ExcelExchangeTools excel = new ExcelExchangeTools(attri.AssetPath, ExcelExchangeTools.OpType.OnlyRead);
        if(!excel.InitSuccess) return;
        
        int idX = -1;
        int idY = -1;
        var content = excel.GetJson(ref idX, ref idY);
        Debug.Log($"Excel 内容 结果： {content.Item2}：： idx = {idX};idy = {idY}  content={content.Item1}");
            
            
        //filed
        var f = t.GetField("Datas", BindingFlags.NonPublic | BindingFlags.Instance);
        Debug.Log(f);

        if (f != null && f.FieldType.GenericTypeArguments.Length > 0)
        {
            var childT = f.FieldType.GenericTypeArguments[0];
             
            Type listType = typeof(List<>).MakeGenericType(childT);

            var value = JsonConvert.DeserializeObject(content.Item1, listType);
            Debug.Log(value);
                
            f.SetValue(v,value);
            EditorUtility.SetDirty(v);
            AssetDatabase.Refresh();
        }
    }


}


[CustomEditor(typeof(ModelManager))]
// [CanEditMultipleObjects]
public class ModelManagerEditor : Editor
{
    private ModelManager ttt;
    
    private void OnEnable()
    {
        ttt = target as ModelManager;
    }

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        if (GUILayout.Button("拉取所有配置"))
        {

            var all = AssetDatabase.FindAssets($"t:{typeof(BaseModel)}");
            var allPath = all.Select(s => AssetDatabase.GUIDToAssetPath(s));
            Debug.Log(JsonConvert.SerializeObject(allPath));

            var f = typeof(ModelManager).GetField("list全数据", BindingFlags.NonPublic | BindingFlags.Instance);
            var datas = f.GetValue( ttt) as List<BaseModel>;
            datas = new List<BaseModel>();
            datas.Clear();
            foreach (var VARIABLE in allPath)
            {
                BaseModel b = AssetDatabase.LoadAssetAtPath<BaseModel>(VARIABLE);
                BaseModelEditor.拉取数据(b);
                datas.Add(b);
            }
            f.SetValue(ttt,datas);
            
            EditorUtility.SetDirty(ttt);
            AssetDatabase.Refresh();
        }
    }


}
